package formBean;

import java.util.ArrayList;
import java.util.List;

import org.mybeans.form.FormBean;

import util.Util;

public class CustomerAc extends FormBean {
	
	private String firstName;	
	private String lastName;	
	private String userName;	
	private String password;	
	private String confirm;	
	private String address1;	
	private String address2;	
	private String city;	
	private String state;	
	private String zipCode;	
	private String phoneNo;	
	private long cash;	
	private String cashAmount;
	
	
	public String getFirstName() {
		return firstName;
	}
	
	public String getLastName() {
		return lastName;
	}
	
	public String getUserName() {
		return userName;
	}
	
	public String getPassword() {
		return password;
	}
	
	public String getConfirm() {
		return confirm;
	}

	public String getAddress1() {
		return address1;
	}
	
	public String getAddress2() {
		return address2;
	}
	
	public String getZipCode() {
		return zipCode;
	}
	
	public String getPhoneNo() {
		return phoneNo;
	}
	
	public String getCashAmount() {
		return cashAmount;
	}

	public void setFirstName(String s) {
		firstName = trimAndConvert(s, "<>\"");
	}
	
	public void setLastName(String s) {
		lastName = trimAndConvert(s, "<>\"");
	}
	
	public void setUserName(String s) {
		userName = trimAndConvert(s, "<>\"");
	}
	
	public void setPassword(String s) {
		password = trimAndConvert(s, "<>\"");
	}
	
	public void setConfirm(String s) {
		confirm = trimAndConvert(s, "<>\"");
	}
	
	public void setAddress1(String s) {
		address1 = trimAndConvert(s, "<>\"");
	}
	
	public void setAddress2(String s) {
		address2 = trimAndConvert(s, "<>\"");
	}
	

	public long getCash() {
		return cash;
	}

	public void setCash(long cash) {
		this.cash = cash;
	}
	
	public void setZipCode(String s) {
		zipCode = trimAndConvert(s, "<>\"");
	}
	
	public void setPhoneNo(String s) {
		phoneNo = trimAndConvert(s, "<>\"");
	}
	
	public void setCashAmount(String s) {
		cashAmount = trimAndConvert(s, "<>\"");
	}
	
	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = trimAndConvert(city, "<>\"");
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = trimAndConvert(state, "<>\"");
	}
	
	@Override
	public List<String> getValidationErrors() {
		ArrayList<String> errors = new ArrayList<String>();
		
		Util.checkNullAndLength("First name", firstName, 15, errors);
		Util.checkNullAndLength("Last name", lastName, 15, errors);
		Util.checkNullAndLength("User name", userName, 15, errors);
		Util.checkNullAndLength("Password", password, 40, errors);
		Util.checkNullAndLength("Address line1", address1, 20, errors);
		if (this.address2 != null && address2.length() > 20) {
			errors.add("Address line2 should have no more than 20 characters.");
		}
		
		if (confirm == null || confirm.length() == 0) {
			errors.add("Please Re-enter the password");
		}
		
		Util.checkNullAndLength("City", city, 15, errors);
		Util.checkNullAndLength("State", state, 10, errors);
		
		if (zipCode == null || zipCode.length() == 0) {
			errors.add("Zip Code is required");
		} 
		
		if (phoneNo == null || phoneNo.length() == 0) {
			errors.add("Phone No. is required");
		} 

		if (cashAmount == null || cashAmount.length() == 0) {
			errors.add("Cash Amount is required");
		} 

		if (errors.size()> 0) {
			return errors;
		}
		
		if (!Util.isValidUsername(this.firstName)) {
			errors.add("First name is not a valid one. It should not contain special characters and should at least have 3 characters");
		}
		
		if (!Util.isValidUsername(this.lastName)) {
			errors.add("Last name is not a valid one. It should not contain special characters and should at least have 3 characters");
		}
		
		if (!Util.isValidUsername(this.userName)) {
			errors.add("User name is not a valid one. It should not contain special characters and should at least have 3 characters");
		}
		
		if (!Util.isValidCity(this.city)) {
			errors.add("City is not a valid one. It should not contain special characters and should at least have 2 characters");
		}
		
		if (!Util.isValidAddress(this.address1)) {
			errors.add("Address line1 is not a valid one. It should not contain special characters.");
		}
		
		if (this.address2 != null && this.address2.length() > 0 && !Util.isValidAddress(this.address2)) {
			errors.add("Address line2 is not a valid one. It should not contain special characters.");
		}
		
		if (!Util.isValidMoneyAmount(this.cashAmount)) {
			errors.add("Cash amount should be less than 1,000,000 with only two decimal places.");
		}
		
		if (!util.Util.isValidZip(zipCode)) errors.add("Zip code should be 5 numbers.");
		
		if (!Util.isValidPhoneNumber(this.phoneNo)) {
			errors.add("Phone No. should be formatted as xxx-xxx-xxxx.");
		}
				
		try {
			this.cash = (long) (Double.parseDouble(cashAmount) * 100);
			if (cash < 0) errors.add("Initial cash amount should not be less than 0");
		} catch (NumberFormatException e) {
			errors.add("Cash amount should be a number with only two decimal places.");
		}		
		
		if (!password.equals(confirm)) {
		    errors.add("Passwords are not the same");
		}
		
		if (errors.size() > 0) return errors;
		
		Util.checkInjection("firstName", firstName, errors);
		Util.checkInjection("lastName", lastName, errors);
		Util.checkInjection("userName", userName, errors);
		Util.checkInjection("password", password, errors);
		Util.checkInjection("address1", address1, errors);
		if (address2 != null) Util.checkInjection("address2", address2, errors);
		Util.checkInjection("city", city, errors);
		Util.checkInjection("state", state, errors);
		Util.checkInjection("zipCode", zipCode, errors);
		Util.checkInjection("phoneNo", phoneNo, errors);			
		
		return errors;
	}
	

}
